package com.client.window;

import com.client.service.OICQClient;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
/**
 * @Author e4glet
 * 2025年3月5日
 * */

// 注册窗口
public class RegisterWindow extends JFrame {
    private JTextField emailField;
    private JTextField nicknameField;
    private JPasswordField passwordField;
    private JButton registerButton;
    private OICQClient client;

    // 注册窗口构造器用于初始化
    public RegisterWindow(OICQClient client) {
        this.client = client;
        setTitle("注册");
        setSize(300, 250);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel();
        add(panel);
        placeComponents(panel);
        setResizable(false); // 设置窗口不可调整大小
        setVisible(true);
    }

    // 添加注册窗口界面组件
    private void placeComponents(JPanel panel) {
        panel.setLayout(null);

        JLabel emailLabel = new JLabel("邮箱:");
        emailLabel.setBounds(10, 20, 80, 25);
        panel.add(emailLabel);

        emailField = new JTextField(20);
        emailField.setBounds(100, 20, 165, 25);
        panel.add(emailField);

        JLabel nicknameLabel = new JLabel("昵称:");
        nicknameLabel.setBounds(10, 50, 80, 25);
        panel.add(nicknameLabel);

        nicknameField = new JTextField(20);
        nicknameField.setBounds(100, 50, 165, 25);
        panel.add(nicknameField);

        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(10, 80, 80, 25);
        panel.add(passwordLabel);

        passwordField = new JPasswordField(20);
        passwordField.setBounds(100, 80, 165, 25);
        panel.add(passwordField);

        registerButton = new JButton("注册");
        registerButton.setBounds(100, 120, 80, 25);
        panel.add(registerButton);

        // 添加注册按钮监听器
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String email = emailField.getText().trim();
                String nickname = nicknameField.getText().trim();
                String password = new String(passwordField.getPassword());

                // 输入验证
                if (email.isEmpty() || !email.contains("@")) {
                    JOptionPane.showMessageDialog(null, "请输入有效的邮箱地址");
                    return;
                }
                if (nickname.isEmpty()) {
                    JOptionPane.showMessageDialog(null, "请输入昵称");
                    return;
                }
                if (password.length() < 6) {
                    JOptionPane.showMessageDialog(null, "密码长度至少6个字符");
                    return;
                }

                try {
                    if (client.registerUser(email, nickname, password)) {
                        JOptionPane.showMessageDialog(null, "注册成功");
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "注册失败，邮箱可能已被使用");
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(null, "注册失败：" + ex.getMessage());
                }
            }
        });
    }
}
